package com.flyu.filter;

import javax.servlet.*;
import java.io.IOException;

public class CharacterEncodeFilter implements Filter {
    @Override
    // web服务器启动时初始化
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("CharacterEncodeFilter初始化");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        servletRequest.setCharacterEncoding("utf-8");
        servletResponse.setCharacterEncoding("utf-8");
        servletResponse.setContentType("text/html;charset=utf-8");

        System.out.println("过滤之前");
        filterChain.doFilter(servletRequest, servletResponse);      // 必须执行,如果不写，程序在此被拦截
        System.out.println("过滤之后");
    }

    @Override
    public void destroy() {
        System.out.println("CharacterEncodeFilter已销毁");
    }
}
